<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    const axios = {
        //创建XMLHttpRequest对象
        get: (url,data) => {
                const xhr = new XMLHttpRequest();
                xhr.open('GET', url + '?' + data, true)
                xhr.send(null)
                return new Promise((resolve, reject) => {
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4) {
                            let status = xhr.status
                            if (status >= 200 && status < 300) {
                                // responseText 后端返回给我们的字符串
                                resolve(JSON.parse(xhr.responseText))
                            } else {
                                reject('请求失败了')
                            }
                        }
                    }
                })
            },
            post: (url,data) => {
                const xhr = new XMLHttpRequest();
                xhr.open('POST', url, true);
                xhr.send(data);
                return new Promise((resolve, reject) => {
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4) {
                            let status = xhr.status
                            if (status >= 200 && status < 300) {
                                // responseText 后端返回给我们的字符串
                                resolve(JSON.parse(xhr.responseText))
                            } else {
                                reject('请求失败了')
                            }
                        }
                    }
                })
            }
        //发送请求
        //接收请求
        // onreadystatechange 有4种状态


    }

    // axios({
    //     type: 'GET',
    //     url: 'http://172.23.23.220:3000/axios'
    // }).then(res => {
    //     console.log(res);
    // })
    axios.get('http://172.23.23.220:3000/axios').then(res => {
        console.log(res);
    })

</script>